#include "gamewindow.h"

#include <QKeyEvent>
#include <gestureutils.h>
#include <cstdlib>
#include <ctime>

using namespace std;
using namespace cv;
using namespace mlgesture;

float GameWindow::birdsVelocity_ = 0.0001;
float GameWindow::birdsAcceleration_ = 2e-6;
float GameWindow::planeVelocity_ = 1.0/40000;
float GameWindow::bulletVelocity_ = 0.0004;
int GameWindow::shotTime_ = 500;
int GameWindow::bulletsRestoreTime_ = 1000;
int GameWindow::maxBullets_ = 20;
float GameWindow::planeWidth_ = 0.2;
float GameWindow::planeHeight_ = 0.1;

GameWindow::GameWindow(QWidget *parent, QGLWidget *share_widget) :
	QGLWidget(parent, share_widget)
{
	setFocusPolicy(Qt::StrongFocus);
	isImgPanel_ = false;
	isMaskPanel_ = false;
	windowWidth_ = 640;
	windowHeight_ = 480;
	setMinimumSize(windowWidth_, windowHeight_);
	restart();

	glGenTextures(1, &backgroundTexture_);
	glGenTextures(1, &planeTexture_);
	glGenTextures(1, &cameraTexture_);
	glGenTextures(1, &maskTexture_);
	glGenTextures(1, &hpTexture_);
	glGenTextures(2, birdsTexture_);
	glGenTextures(1, &bulletTexture_);
	glGenTextures(1, &cityTexture_);
	glGenTextures(1, &scoreTexture_);
	glGenTextures(1, &bulletIconTexture_);

	FMOD::System_Create(&fmodSystem_);
	fmodSystem_->init(32, FMOD_INIT_NORMAL,NULL);
	fmodSystem_->createSound("handPlane_data/sound/explode1.mp3", FMOD_LOOP_OFF, 0, &sounds_[0]);
	fmodSystem_->createSound("handPlane_data/sound/explode2.mp3", FMOD_LOOP_OFF, 0, &sounds_[1]);
	fmodSystem_->createSound("handPlane_data/sound/shoot.mp3", FMOD_LOOP_OFF, 0, &sounds_[2]);
	fmodSystem_->createSound("handPlane_data/sound/plane.mp3", FMOD_DEFAULT, 0, &sounds_[3]);
	fmodSystem_->playSound(FMOD_CHANNEL_FREE, sounds_[3], true, &planeChannel_);
	planeChannel_->setVolume(0.8f);
}

GameWindow::~GameWindow() {
	fmodSystem_->release();
	glDeleteTextures(1, &backgroundTexture_);
	glDeleteTextures(1, &planeTexture_);
	glDeleteTextures(1, &cameraTexture_);
	glDeleteTextures(1, &maskTexture_);
	glDeleteTextures(1, &hpTexture_);
	glDeleteTextures(2, birdsTexture_);
	glDeleteTextures(1, &bulletTexture_);
	glDeleteTextures(1, &cityTexture_);
	glDeleteTextures(1, &scoreTexture_);
	glDeleteTextures(1, &bulletIconTexture_);
}

void GameWindow::restart() {
	srand(time(0));
	planePos_ = 0;
	planeAngle_ = 0;
	cityValue_ = 1.0;
	hpValue_ = 1.0;
	birds_.clear();
	bullets_.clear();
	nextBird_ = 0;
	score_ = 0;
	bulletsNum_ = maxBullets_;
	bulletAdded_ = false;
	nextShot_ = 0;
	nextRestore_ = bulletsRestoreTime_;
	planeChannel_->setPosition(0, FMOD_TIMEUNIT_MS);
}

void GameWindow::renderContext(unsigned int elapsed_time) {
	if (isRunning_) {
		bool a = isImgPanel_ || isMaskPanel_;
		for (vector<Bird>::iterator it = birds_.begin(); it != birds_.end();) {
			it->posZ += birdsVelocity_*elapsed_time;
			if (it->posZ > -0.1) {
				if (transformWidth(abs(it->posX - planePos_),a) < transformWidth(planeWidth_ + 0.10,a)) {
					hpValue_ -= 0.1;
					fmodSystem_->playSound(FMOD_CHANNEL_FREE, sounds_[1], false, &otherChannel_);
					if (hpValue_ <= 0) {
						planeChannel_->setPaused(true);
						emit emitScore(score_);
						emit emitPurpose(tr("Zostałeś zniszczony"));
						emit endGame();
					}
				}
				else {
					cityValue_ -= 0.1;
					fmodSystem_->playSound(FMOD_CHANNEL_FREE, sounds_[1], false, &otherChannel_);
					if (cityValue_ <= 0)  {
						planeChannel_->setPaused(true);
						emit emitScore(score_);
						emit emitPurpose(tr("Miasto zostało zniszczone"));
						emit endGame();
					}
				}
				it = birds_.erase(it);
			}
			else ++it;
		}
		nextBird_ -= elapsed_time;
		if (nextBird_ < 0) {
			Bird birdTmp;
			birdTmp.posX = (rand()%180)/100.0 - 0.9;
			birdTmp.posZ = -0.5;
			birdTmp.type = rand()%2;
			birds_.push_back(birdTmp);
			nextBird_ = rand()%800 + 1400;
			birdsVelocity_ += birdsAcceleration_;
			//addBullet();
		}


		for (vector<Bullet>::iterator it = bullets_.begin(); it != bullets_.end();) {
			it->posZ -= bulletVelocity_*elapsed_time;
			if (it->posZ < -0.5) {
				it = bullets_.erase(it);
			}
			else ++it;
		}
		planePos_ += planeAngle_*planeVelocity_*elapsed_time;
		if (planePos_ < -0.97) planePos_ = -0.97;
		float rightBarrier = 0.97f;
		if (planePos_ > rightBarrier)
			planePos_ = rightBarrier;

		if (bulletAdded_) {
			nextRestore_ = bulletsRestoreTime_;
		}
		else {
			if (bulletsNum_ < maxBullets_) {
				if (nextRestore_ <= 0) {
					bulletsNum_ += 2;
					if (bulletsNum_ > maxBullets_)
						bulletsNum_ = maxBullets_;
					nextRestore_ = bulletsRestoreTime_;
				}
				else {
					nextRestore_ -= elapsed_time;
				}
			}
		}
		if (nextShot_ > 0)
			nextShot_ -= elapsed_time;
		bulletAdded_ = false;
	}
	collisionProcess();
	fmodSystem_->update();
	updateGL();
}

void GameWindow::setAngle(float angle){
	planeAngle_ = angle*180/3.14;
	if (planeAngle_ > 90) planeAngle_ = 90;
	if (planeAngle_ < -90) planeAngle_ = -90;
}

void GameWindow::initializeGL() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glAlphaFunc(GL_GREATER, 0.1f);
	glEnable(GL_ALPHA_TEST);
	qglClearColor(Qt::black);
	createTexture();
}

void GameWindow::paintGL() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glVertexPointer(3, GL_FLOAT, 0, vertices_.constData());
	glTexCoordPointer(2, GL_FLOAT, 0, texCoords_.constData());
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	bool is_panel = isImgPanel_ || isMaskPanel_;

	if (!isRunning_) {
		glColor4f(0.4f,0.4f,0.4f,1.0f);
	}
	else {
		glColor4f(1.0f,1.0f,1.0f,1.0f);
	}
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -0.49f);
	glBindTexture(GL_TEXTURE_2D, backgroundTexture_);
		glBegin( GL_QUADS );
		//Bottom-left vertex (corner)
		glTexCoord2f( 0, 0 );
		glVertex3f( -5, 5, 0 );

		//Bottom-right vertex (corner)
		glTexCoord2f( 1, 0 );
		glVertex3f( 5, 5, 0 );

		//Top-right vertex (corner)
		glTexCoord2f( 1, 1 );
		glVertex3f( 5, -5, 0 );

		//Top-left vertex (corner)
		glTexCoord2f( 0, 1 );
		glVertex3f( -5, -5, 0 );
	glEnd();

	glLoadIdentity();
	glTranslatef(transformPos(planePos_,is_panel), 0.4f, -0.1001f);
	glRotatef(planeAngle_, 0.0f, 0.0f, 1.0f);
	glTranslatef(-transformWidth(planeWidth_,is_panel), -planeHeight_, 0.0f);
	glBindTexture(GL_TEXTURE_2D, planeTexture_);
	glBegin( GL_QUADS );
		//Bottom-left vertex (corner)
		glTexCoord2f( 0, 0 );
		glVertex3f( 0, 2*planeHeight_, 0 );

		//Bottom-right vertex (corner)
		glTexCoord2f( 1, 0 );
		glVertex3f( transformWidth(2*planeWidth_,is_panel), 2*planeHeight_, 0 );

		//Top-right vertex (corner)
		glTexCoord2f( 1, 1 );
		glVertex3f( transformWidth(2*planeWidth_,is_panel), 0, 0 );

		//Top-left vertex (corner)
		glTexCoord2f( 0, 1 );
		glVertex3f( 0, 0, 0 );
	glEnd();

	//Rysowanie poziomu hp
	float hpLen = transformWidth(0.9 * hpValue_, is_panel);
	glLoadIdentity();
	glTranslatef(transformPos(-0.9,is_panel), 0.95f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, hpTexture_);
	glBegin( GL_QUADS );
		//Bottom-left vertex (corner)
		glTexCoord2f( 0, 0 );
		glVertex3f( 0, 0, -0.1 );

		//Bottom-right vertex (corner)
		glTexCoord2f( 1, 0 );
		glVertex3f( hpLen, 0, -0.1 );

		//Top-right vertex (corner)
		glTexCoord2f( 1, 1 );
		glVertex3f( hpLen, -0.06, -0.1 );

		//Top-left vertex (corner)
		glTexCoord2f( 0, 1 );
		glVertex3f( 0, -0.06, -0.1 );
	glEnd();

	//Rysowanie poziomu zniszczenia miasta
	float cityLen = transformWidth(0.9 * cityValue_, is_panel);
	glLoadIdentity();
	glTranslatef(transformPos(-0.9,is_panel), 0.87f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, cityTexture_);
	glBegin( GL_QUADS );
		//Bottom-left vertex (corner)
		glTexCoord2f( 0, 0 );
		glVertex3f( 0, 0, -0.1 );

		//Bottom-right vertex (corner)
		glTexCoord2f( 1, 0 );
		glVertex3f( cityLen, 0, -0.1 );

		//Top-right vertex (corner)
		glTexCoord2f( 1, 1 );
		glVertex3f( cityLen, -0.06, -0.1 );

		//Top-left vertex (corner)
		glTexCoord2f( 0, 1 );
		glVertex3f( 0, -0.06, -0.1 );
	glEnd();

	//Rysowanie ptaków
	for (vector<Bird>::iterator it = birds_.begin(); it != birds_.end(); ++it) {
		glLoadIdentity();
		float birdX = it->posX;
		if (is_panel)
			birdX = computeNewX(it->posX, it->posZ);
		//birdX = transformPos(birdX);
		glTranslatef(birdX, 0.4f, it->posZ);
		glBindTexture(GL_TEXTURE_2D, birdsTexture_[it->type]);
		glBegin( GL_QUADS );
			//Bottom-left vertex (corner)
			glTexCoord2f( 0, 0 );
			glVertex3f( transformWidth(-0.1,is_panel), 0.1, 0 );

			//Bottom-right vertex (corner)
			glTexCoord2f( 1, 0 );
			glVertex3f( transformWidth(0.1,is_panel), 0.1, 0 );

			//Top-right vertex (corner)
			glTexCoord2f( 1, 1 );
			glVertex3f( transformWidth(0.1,is_panel), -0.1, 0 );

			//Top-left vertex (corner)
			glTexCoord2f( 0, 1 );
			glVertex3f( transformWidth(-0.1,is_panel), -0.1, 0 );
		glEnd();
	}

	//Rysowanie kul
	for (vector<Bullet>::iterator it = bullets_.begin(); it != bullets_.end(); ++it) {
		glLoadIdentity();
		float bulletX = it->posX;
		if (is_panel)
			bulletX = computeNewX(it->posX, it->posZ);
		glTranslatef(bulletX, 0.4f, it->posZ);
		glBindTexture(GL_TEXTURE_2D, bulletTexture_);
		glBegin( GL_QUADS );
			//Bottom-left vertex (corner)
			glTexCoord2f( 0, 0 );
			glVertex3f( transformWidth(-0.05,is_panel), 0.05, 0 );

			//Bottom-right vertex (corner)
			glTexCoord2f( 1, 0 );
			glVertex3f( transformWidth(0.05,is_panel), 0.05, 0 );

			//Top-right vertex (corner)
			glTexCoord2f( 1, 1 );
			glVertex3f( transformWidth(0.05,is_panel), -0.05, 0 );

			//Top-left vertex (corner)
			glTexCoord2f( 0, 1 );
			glVertex3f( transformWidth(-0.05,is_panel), -0.05, 0 );
		glEnd();
	}

	//Rysowanie obrazu z kamery
	if (isImgPanel_) {
		glLoadIdentity();
		glBindTexture(GL_TEXTURE_2D, cameraTexture_);
		glBegin( GL_QUADS );
			//Bottom-left vertex (corner)
			glTexCoord2f( 0, 0 );
			glVertex3f( 0.33, 0, -0.1 );

			//Bottom-right vertex (corner)
			glTexCoord2f( 1, 0 );
			glVertex3f( 1, 0, -0.1 );

			//Top-right vertex (corner)
			glTexCoord2f( 1, 1 );
			glVertex3f( 1, -1, -0.1 );

			//Top-left vertex (corner)
			glTexCoord2f( 0, 1 );
			glVertex3f( 0.33, -1, -0.1);
		glEnd();
	}

	//Rysowanie obrazu maski
	if (isMaskPanel_) {
		glLoadIdentity();
		glBindTexture(GL_TEXTURE_2D, maskTexture_);
		glBegin( GL_QUADS );
			//Bottom-left vertex (corner)
			glTexCoord2f( 0, 0 );
			glVertex3f( 0.33, 1, -0.1 );

			//Bottom-right vertex (corner)
			glTexCoord2f( 1, 0 );
			glVertex3f( 1, 1, -0.1 );

			//Top-right vertex (corner)
			glTexCoord2f( 1, 1 );
			glVertex3f( 1, 0, -0.1 );

			//Top-left vertex (corner)
			glTexCoord2f( 0, 1 );
			glVertex3f( 0.33, 0, -0.1 );
		glEnd();
	}
	drawBulletsNum(is_panel);
	drawScore(is_panel);

	glFlush();
}

void GameWindow::resizeGL(int width, int height) {
	float a = 1.0*width/windowWidth_;
	float b = 1.0*height/windowHeight_;
	int posX, posY;
	if (a<b) {
		posX = 0;
		posY = (height - (width * windowHeight_/windowWidth_))/2;
		height = width * windowHeight_/windowWidth_;
	}
	else {
		posY = 0;
		posX = (width - (height * windowWidth_/windowHeight_))/2;
		width = height * windowWidth_/windowHeight_;
	}
	glViewport(posX, posY, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0f, 1.0f, 1.0f, -1.0f, 0.1, 0.5);
	glMatrixMode(GL_MODELVIEW);
}

void GameWindow::createTexture() {
	QImage qframe("handPlane_data/sky.jpg");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, backgroundTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/plane.png");
	planeSize_ = qframe.size();
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, planeTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/hpTexture.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, hpTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/bird1.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, birdsTexture_[0]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/bird2.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, birdsTexture_[1]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/bullet.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, bulletTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/cityTexture.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, cityTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/scoreTexture.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, scoreTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );

	qframe.load("handPlane_data/bulleticon.png");
	qframe = QGLWidget::convertToGLFormat(qframe);
	glBindTexture(GL_TEXTURE_2D, bulletIconTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );
}

void GameWindow::setRunning(bool running) {
	isRunning_ = running;
	planeChannel_->setPaused(!running);
}

void GameWindow::keyPressEvent(QKeyEvent *event) {
	if (event->key() == Qt::Key_Escape) {
		emit closeGame();
	}
}

void GameWindow::setWindowSize(int width, int height) {
	windowWidth_ = width;
	windowHeight_ = height;
	setMinimumSize(windowWidth_, windowHeight_);
	resize(width, height);
}

void GameWindow::setSidePanel(bool img, bool mask) {
	isImgPanel_ = img;
	isMaskPanel_ = mask;
}

void GameWindow::setCameraTextures(const Mat &camera, const Mat &mask) {
	QImage qcamera_frame = GestureUtils::convertMat2QImage(camera);
	qcamera_frame = QGLWidget::convertToGLFormat(qcamera_frame);
	QImage qmask_frame = GestureUtils::convertMat2QImage(mask);
	qmask_frame = QGLWidget::convertToGLFormat(qmask_frame);

	glBindTexture(GL_TEXTURE_2D, cameraTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qcamera_frame.width(), qcamera_frame.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qcamera_frame.bits() );


	glBindTexture(GL_TEXTURE_2D, maskTexture_);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D( GL_TEXTURE_2D, 0, 4, qmask_frame.width(), qmask_frame.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qmask_frame.bits() );
}

float GameWindow::computeNewX(float x, float z) {
	float z2 = -z;
	return 6.7*x*z2 - 3.3*z2;
}

void GameWindow::addBullet() {
	if (nextShot_ <=0 && bulletsNum_>0) {
		Bullet bullet_tmp;
		bullet_tmp.posX = planePos_;
		bullet_tmp.posZ = -0.101;
		bullets_.push_back(bullet_tmp);
		bulletsNum_--;
		nextShot_ = shotTime_;
		fmodSystem_->playSound(FMOD_CHANNEL_FREE, sounds_[2], false, &otherChannel_);
	}
	bulletAdded_ = true;
}

void GameWindow::collisionProcess() {
	for (vector<Bullet>::iterator it = bullets_.begin(); it != bullets_.end();) {
		bool is = false;
		for (vector<Bird>::iterator it2 = birds_.begin(); it2 != birds_.end(); ++it2) {
			float dist = abs(it->posX - it2->posX);
			if (dist <= 0.1 && it->posZ< it2->posZ) {
				birds_.erase(it2);
				it = bullets_.erase(it);
				is = true;
				score_ += 10;
				fmodSystem_->playSound(FMOD_CHANNEL_FREE, sounds_[0], false, &otherChannel_);
				break;
			}
		}
		if (!is) ++it;
	}
}

void GameWindow::drawBulletsNum(bool is_panel) {
	int num = bulletsNum_;
	glLoadIdentity();
	glTranslatef(transformPos(0.6, is_panel), 0.93, -0.1);
	glBindTexture(GL_TEXTURE_2D,bulletIconTexture_);
	glBegin( GL_QUADS );
		//Bottom-left vertex (corner)
		glTexCoord2f( 0.0, 0 );
		glVertex3f( 0, 0, 0 );

		//Bottom-right vertex (corner)
		glTexCoord2f( 1.0, 0 );
		glVertex3f(transformWidth(0.1, is_panel), 0, 0 );

		//Top-right vertex (corner)
		glTexCoord2f( 1.0, 1 );
		glVertex3f( transformWidth(0.1, is_panel), -0.1, 0 );

		//Top-left vertex (corner)
		glTexCoord2f( 0.0, 1 );
		glVertex3f( 0, -0.1, 0 );
	glEnd();

	glLoadIdentity();
	glTranslatef(transformPos(0.9, is_panel), 0.93, -0.1);
	glBindTexture(GL_TEXTURE_2D,scoreTexture_);
	int cyfraPos = 0;
	while (num > 0 || (num==0 &&cyfraPos==0)) {
		int cyfra = num%10;
		glTranslatef(transformWidth(-0.055, is_panel), 0.0f, 0.0f);

		glBegin( GL_QUADS );
			//Bottom-left vertex (corner)
			glTexCoord2f( 0.1*cyfra, 0 );
			glVertex3f( 0, 0, 0 );

			//Bottom-right vertex (corner)
			glTexCoord2f( 0.1*cyfra +0.1, 0 );
			glVertex3f( transformWidth(0.05, is_panel), 0, 0 );

			//Top-right vertex (corner)
			glTexCoord2f( 0.1*cyfra + 0.1, 1 );
			glVertex3f( transformWidth(0.05, is_panel), -0.1, 0 );

			//Top-left vertex (corner)
			glTexCoord2f( 0.1*cyfra, 1 );
			glVertex3f( 0, -0.1, 0 );
		glEnd();
		num /= 10;
		cyfraPos++;
	}
}

void GameWindow::drawScore(bool is_panel) {
	int num = score_;
	glLoadIdentity();
	glTranslatef(transformPos(0.9, is_panel), -0.83, -0.1);
	glBindTexture(GL_TEXTURE_2D,scoreTexture_);
	int cyfraPos = 0;
	while (num > 0 || (num==0 &&cyfraPos==0)) {
		int cyfra = num%10;
		glTranslatef(transformWidth(-0.055, is_panel), 0.0f, 0.0f);

		glBegin( GL_QUADS );
			//Bottom-left vertex (corner)
			glTexCoord2f( 0.1*cyfra, 0 );
			glVertex3f( 0, 0, 0 );

			//Bottom-right vertex (corner)
			glTexCoord2f( 0.1*cyfra +0.1, 0 );
			glVertex3f( transformWidth(0.05, is_panel), 0, 0 );

			//Top-right vertex (corner)
			glTexCoord2f( 0.1*cyfra + 0.1, 1 );
			glVertex3f( transformWidth(0.05, is_panel), -0.1, 0 );

			//Top-left vertex (corner)
			glTexCoord2f( 0.1*cyfra, 1 );
			glVertex3f( 0, -0.1, 0 );
		glEnd();
		num /= 10;
		cyfraPos++;
	}
}

float GameWindow::transformPos(float val, bool is_panel) {
	return (val+1) * (is_panel?1.33:2)/2 -1;
}

float GameWindow::transformWidth(float val, bool is_panel) {
	return val * (is_panel?1.33:2)/2;
}
